﻿using B.S.Meeting.Domain;
using B.S.Meeting.ErrorCode;
using B.S.Meeting.WriteAPI.Applications.Commands.Roles;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace B.S.Meeting.WriteAPI.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class RoleController : ControllerBase
    {
        private readonly IMediator _mediator;
        private readonly ILogger<RoleController> _logger;

        public RoleController(IMediator mediator, ILogger<RoleController> logger)
        {
            _mediator = mediator;
            _logger = logger;
        }

        /// <summary>
        /// 添加新角色
        /// </summary>
        /// <param name="roleName">角色名称</param>
        /// <param name="deptDesc">部门描述</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<int>> AddRole(string roleName, string? deptDesc)
        {
            try
            {
                var cmd = new AddRoleCommand
                {
                    RoleName = roleName,
                    DeptDesc = deptDesc
                };
                _logger.LogInformation($"添加新角色：{roleName}，职务描述：{deptDesc}");
                var result = await _mediator.Send(cmd);
                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "添加角色失败");
                return new APIResult<int>
                {
                    Code = StatusEnumCode.Fail,
                    Message = "添加角色失败",
                    Data = 0
                };
            }
        }

        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="roleId">角色ID</param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<APIResult<int>> DelRole(int roleId)
        {
            try
            {
                _logger.LogInformation($"尝试删除角色: ID: {roleId}");
                var cmd = new DelRoleCommand
                {
                    RoleId = roleId
                };
                var result = await _mediator.Send(cmd);
                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "删除角色失败");
                return new APIResult<int>
                {
                    Code = StatusEnumCode.Fail,
                    Message = "删除角色失败",
                    Data = 0
                };
            }
        }

        /// <summary>
        /// 修改角色
        /// </summary>
        /// <param name="command">角色更新信息</param>
        /// <returns></returns>
        [HttpPut]
        public async Task<APIResult<int>> UpdRole([FromBody] UpdRoleCommand command)
        {
            try
            {
                if (command == null)
                {
                    return new APIResult<int>
                    {
                        Code = StatusEnumCode.Fail,
                        Message = "请求参数不能为空",
                        Data = 0
                    };
                }

                _logger.LogInformation($"尝试修改角色: ID: {command.RoleId}, 名称: {command.RoleName}, 描述: {command.DeptDesc}");
                var result = await _mediator.Send(command);
                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"修改角色失败: ID {command?.RoleId}");
                return new APIResult<int>
                {
                    Code = StatusEnumCode.Fail,
                    Message = "修改角色失败",
                    Data = 0
                };
            }
        }

        /// <summary>
        /// 设置角色状态
        /// </summary>
        /// <param name="roleId">角色ID</param>
        /// <param name="roleStatus">角色状态</param>
        /// <returns></returns>
        [HttpPut]
        public async Task<APIResult<int>> UpdRoleStatus(int roleId,int roleStatus)
        {
            try
            {
                UpdRoleStateCommand cmd = new UpdRoleStateCommand
                {
                    RoleId=roleId,
                    RoleStatus=roleStatus
                };
                var result = await _mediator.Send(cmd);
                return result;
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
